<template>
  <div class="home">
    <el-row :gutter="20">
      <!-- 统计卡片 -->
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>待诊人数</span>
              <el-tag type="warning" size="small">实时</el-tag>
            </div>
          </template>
          <div class="card-content">
            <h2>{{ stats.waitingPatients }}</h2>
            <el-button type="primary" size="small">查看详情</el-button>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>今日接诊</span>
              <el-tag type="success" size="small">今日</el-tag>
            </div>
          </template>
          <div class="card-content">
            <h2>{{ stats.todayPatients }}</h2>
            <el-button type="primary" size="small">查看详情</el-button>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>总接诊量</span>
              <el-tag type="info" size="small">累计</el-tag>
            </div>
          </template>
          <div class="card-content">
            <h2>{{ stats.totalPatients }}</h2>
            <el-button type="primary" size="small">查看详情</el-button>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>预约数量</span>
              <el-tag type="primary" size="small">今日</el-tag>
            </div>
          </template>
          <div class="card-content">
            <h2>{{ stats.appointments }}</h2>
            <el-button type="primary" size="small">查看详情</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 欢迎信息 -->
    <el-card class="welcome-card" style="margin-top: 20px">
      <template #header>
        <div class="card-header">
          <span>欢迎回来，{{ userInfo?.username || '医生' }}</span>
        </div>
      </template>
      <div class="welcome-content">
        <p>今天是 {{ new Date().toLocaleDateString() }}，祝您工作愉快！</p>
        <el-row :gutter="20" style="margin-top: 20px" justify="center">
          <el-col :span="6">
            <el-button type="primary" block @click="goToReception">开始接诊</el-button>
          </el-col>
          <el-col :span="6">
            <el-button type="success" block>查看预约</el-button>
          </el-col>
          <el-col :span="6">
            <el-button type="warning" block>查看统计</el-button>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useUserStore } from '../stores/user'
import { useRouter } from 'vue-router'
import request from '../utils/request'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const userInfo = computed(() => userStore.userInfo)

// 统计数据 - 初始化为默认值
const stats = ref({
  waitingPatients: 0,
  todayPatients: 0,
  totalPatients: 0,
  appointments: 0
})
const loadingStats = ref(false)

const goToReception = () => {
  router.push({ name: 'doctor-outpatient-reception' });
}

// 获取统计数据的方法
const fetchDashboardStats = async () => {
  loadingStats.value = true;
  try {
    const res = await request.get('/api/patient/doctor/actions/dashboard-stats/');
    if (res.data) {
      stats.value = res.data;
    } else {
       ElMessage.warning('获取统计数据格式有误');
    }
  } catch (error) {
    console.error('获取首页统计数据失败:', error);
    ElMessage.error('获取首页统计数据失败');
  } finally {
    loadingStats.value = false;
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchDashboardStats();
})
</script>

<style scoped>
.home {
  min-height: 100%;
  padding: 20px;
  background-color: #f0f2f5;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-content {
  text-align: center;
  padding: 10px 0;
}

.card-content h2 {
  font-size: 28px;
  color: #303133;
  margin: 10px 0;
}

.welcome-card {
  margin-top: 20px;
  background-color: #fff;
}

.welcome-content {
  font-size: 16px;
  color: #606266;
  line-height: 1.5;
}

.welcome-content p {
  margin: 10px 0;
}

:deep(.el-card__header) {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-card__body) {
  padding: 20px;
}

:deep(.el-button) {
  margin-top: 10px;
}

.el-row {
  margin-bottom: 20px;
}

.el-col {
  margin-bottom: 20px;
}

.card-content h2.loading {
  color: #909399;
}
</style> 